1 using UnityEngine;
2 using
System.Collections;
3
4 public
class EnemyHealth : MonoBehaviour {
5
6     
public int startingHealth = 100;
7     
public int currentHealth = 100;
8
9     
public int pointValueOnKill = 10;
10
11     
bool isDead = false;
12     Animator anim;
13     UnityEngine.AI.NavMeshAgent agent;
14
15     ParticleSystem hitParticles;
16
17     AudioSource audio;
18     
public AudioClip hurt1;
19     
public AudioClip hurt2;
20     
public AudioClip hurt3;
21     
public AudioClip scoreUp;
22
23
24     
// Use this for initialization
25     
void Start () {
26         currentHealth = startingHealth;
27         anim = GetComponent<Animator> ();
28         agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
29         hitParticles = GetComponent<ParticleSystem> ();
30         audio = GetComponent<AudioSource> ();
31     }
32     
33     
// Update is called once per frame
34     
void Update () {
35     
36     }
37
38
39     
public void TakeDamage(int amount, Vector3 hitPoint){
40         
// we need to find out if the enemy is already or not
41         
if (isDead)
42             
return;
43
44         currentHealth -= amount;
45         hitParticles.transform.position = hitPoint;
46         hitParticles.Play ();
47         
if (currentHealth <= 0) {
48             
49             GameObject player = GameObject.FindGameObjectWithTag (
"Player");
50             IronManBehaviorScript playerScript = player.GetComponent<IronManBehaviorScript> ();
51             playerScript.score += pointValueOnKill;
52             audio.PlayOneShot (scoreUp,
1.0f);
53             Death ();
54         }
55
56         
int randomNumber = Random.Range (1, 3);
57         
switch (randomNumber) {
58         
case 1:
59             audio.PlayOneShot (hurt1,
0.3f);
60             
break;
61
62         
case 2:
63             audio.PlayOneShot (hurt2,
0.3f);
64             
break;
65
66         
case 3:
67             audio.PlayOneShot (hurt3,
0.3f);
68             
break;
69
70         }
71     }
72
73     
public void Death(){
74         anim.SetTrigger (
"death");
75         
//agent.enabled = false;
76         agent.speed =
0;
77         isDead =
true;
78         Destroy (gameObject,
2.5f);
79     }
80         
81 }


Gõ tìm kiếm nhanh...